<template>
	<view class="">
		<scroll-view class="apply-for" :refresher-enabled="true" :refresher-triggered="triggered"
			:refresher-threshold="100" @refresherrefresh="refresherpulling">
			<view class="apply-detail">
				<view class="apply-info">
					<view class="info-label">单位类型</view>
					<view class="info-input">
						<uni-data-select v-model="positionType" :localdata="range" :placeholder="placeholder"
							@change="change"></uni-data-select>
					</view>
				</view>
				<button @click="submit">提交</button>
				<view class="agreement">
					<view class="agreement-model">
						<u-checkbox-group @change="changeAgreement" class="radio-box">
							<u-checkbox :model="agreement" shape="circle" label=" " name="1"></u-checkbox>
						</u-checkbox-group>

						<view class="">
							<text>请先阅读隐私协议，勾选则代表同意协议内容</text>
							<text class="agreement-name" @click.stop="openPrivacy">《{{protocolTitle}}》</text>
						</view>

					</view>
				</view>
			</view>
			<image src="../../static/images/home/weibaoBg.jpg" mode="" class="apply-img"></image>
		</scroll-view>
	</view>
</template>

<script>
	import Home from '@/api/home.js'
	import Common from "@/api/common.js"
	import {
		getToken
	} from "@/utils/auth"
	export default {
		data() {
			return {
				scrollHeight: '',
				positionType: 1,
				triggered: false,
				range: [],
				protocolTitle: '',
				agreementsContent: '',
				agreement: false,
			}
		},
		onLoad() {
			// this.getstates()
			this.scrollHeight = this.$store.getters.heightData.noTabMainHeight - 74 + 'px'
			this.getUnitTypeList()
			this.getAgreements()
		},
		methods: {
			//获取状态
			getstates() {
				Home.statesMaintainApply().then(res => {
					if (res.code == 200) {
						// 0未申请 1审核中 2通过 3未通过
						if (res.data) {
							this.applyStatus = res.data?.auditType || 0
							if (res.data.auditType == 1) {
								uni.redirectTo({
									url: '/subPages/home/master/notExamine?title=维保申请&msg=请您耐心等待'
								})
							} else {
								this.applyStatus = 2
							}
						}
					}
				})
			},
			// 获取单位类型
			getUnitTypeList() {
				Home.entType().then(res => {
					this.range = []
					if (res.code == 200) {
						res.data.map(item => {
							this.range.push({
								value: Number(item.dictValue),
								text: item.dictLabel
							})
						})
					}
				})
			},
			change(e) {
				console.log("e:", e);
			},
			formatterImage(data) {
				if (data == "") return "";
				var jsonDa = JSON.stringify(data).replace(/<img/gi,
					"<img class='richImg'style='width:100%!important;height:auto!important;max-height:100%;'");
				var newResData = JSON.parse(jsonDa);
				return newResData;
			},
			changeAgreement(v) {
				if (v.length) {
					this.agreement = true
				} else {
					this.agreement = false
				}
			},
			submit() {
				if (!this.agreement) {
					uni.showToast({
						title: '请先阅读隐私协议并勾选',
						icon: 'none',
					})
					return
				}
				if (this.positionType == '') {
					uni.showToast({
						title: '企业类型不能为空',
						icon: 'none'
					})
					return
				}
				Home.addMaintainApply({
					entType: this.positionType,
				}).then(res => {
					if (res.code == 200) {
						uni.showToast({
							icon: "none",
							title: "申请成功"
						})
						setTimeout(() => {
							uni.switchTab({
								url: '/pages/home'
							})
						}, 1000)
					}
				})
			},
			openPrivacy() {
				uni.navigateTo({
					url: `/subPages/mine/publicAgreement?id=${'9'}`
				})
			},
			async getAgreements() {
				let res = await Common.getAgreements('9');
				console.log(res, 'res');
				this.protocolTitle = res.data.title
				this.agreementsContent = this.formatterImage(res.data?.content);
			},
			refresherpulling() {
				this.triggered = true
				setTimeout(() => {
					this.triggered = false
				}, 1000)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.apply-for {
		width: 100vw;
		height: 100vh;

		.apply-img {
			width: 100%;
			height: 100%;
		}

		.apply-detail {
			height: 50%;
			width: 100%;
			position: absolute;
			bottom: 10px;
			left: 0;
			z-index: 99;
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: space-between;
			padding: 30rpx 10%;
			padding-bottom: 50rpx;

			.apply-info {
				display: flex;
				align-items: center;
				width: inherit;
				height: 50%;

				.info-label {
					margin-right: 20rpx;
					color: #fff;
				}

				.info-input {
					flex: 1;
					width: 100%;


					.uni-input {
						background-color: #fff;
						color: black;
						height: 80rpx;
						width: inherit;
						border-radius: 15rpx;
						border: 1px solid #999;
						padding-left: 10rpx;
					}

					.info-select {
						background-color: #fff !important;
					}

				}
			}

			button {
				width: 50%;
				height: 80rpx;
				background-color: $main-color;
				color: #fff;
				line-height: 80rpx;
			}

			.agreement {
				.agreement-model {
					display: flex;
					width: 100%;

					/deep/.radio-box {
						width: 50rpx;
						height: 50rpx;

						.raio-item {}
					}

					text {
						font-size: 24rpx;
						color: $main-font-auxiliary;
					}

					.agreement-name {
						color: $main-color;
					}

				}
			}
		}
	}

	/deep/.uni-select {
		background-color: #fff !important;
	}
</style>